package com.cy.ppj.web.admin.finance;

import com.cy.ppj.service.account.CommissionRecordService;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.ExportExcelUtils;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.enums.CommissionType;
import com.cy.ppj.model.query.commission.CommissionRecordAdminQuery;
import com.cy.ppj.model.vo.admin.commission.CommissionRecordAdminVO;
import com.cy.ppj.model.vo.admin.commission.CommissionTypeVO;
import com.cy.ppj.model.vo.admin.order.OrderCommissionAdminStatisticsVO;
import com.cy.ppj.service.account.CommissionService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-佣金发放记录
 * @author: tanyilin
 * @create: 2018年8月4日 下午5:21:58
 */
@Slf4j
@RestController
@RequestMapping("/admin/commission")
@Api(tags = "后台-佣金发放记录")
public class AdminCommissionController extends BaseController{

	@Autowired
	private CommissionService commissionService;
	@Autowired
	private CommissionRecordService commissionRecordService;
	
	@ApiOperation("佣金发放记录")
	@GetMapping("/list")
	public PageBean<CommissionRecordAdminVO> list(CommissionRecordAdminQuery query) {
		PageBean<CommissionRecordAdminVO> pageBean = commissionRecordService.selectCommissionRecordAdminList(query);
		return pageBean;
	}
	
	@ApiOperation("/发放佣金")
	@PostMapping("/do/pay")
	public Result doPayCommission(@ApiParam(value = "订单ID", example = "0", required = true) @RequestParam Long orderId) {
		commissionService.payCommission2(orderId);
		return ok();
	}
	
	@ApiOperation("佣金统计")
	@GetMapping("/statistics")
	public Result<OrderCommissionAdminStatisticsVO> statistics() {
		OrderCommissionAdminStatisticsVO vo = commissionRecordService.statistics();
		return ok("", vo);
	}
	
	@ApiOperation("佣金类型列表")
	@GetMapping("/type/list")
	public Result<List<CommissionTypeVO>> typeList() {
		return ok("", CommissionType.getTypeList());
	}
	
	@ApiOperation("导出Excel")
	@GetMapping("/list/export/excel")
	public void listExportExcel(CommissionRecordAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<CommissionRecordAdminVO> data = this.list(query).getData();
		String title = "佣金发放记录";
		String[] headers = {"序号", "收款人", "佣金金额", "佣金类型", "客户姓名", "贷款产品", 
				"放款金额", "佣金率", "经纪人", "发放时间"};
		String[] fieldNames = {"id", "realName", "amount", "typeRemark", "customerName", "productName", 
				"loanAmount", "commissionRateText", "recommendUserName", "createTime"};
		ExportExcelUtils<CommissionRecordAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
